package com.zui.manager;


import java.util.ArrayList;
import java.util.List;

import org.displaytag.properties.SortOrderEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.zui.entity.party.Party;
import com.zui.entity.trade.Trade;
import com.zui.security.User;
import com.zui.util.Page;

@SuppressWarnings({"unchecked","rawtypes"})
@Transactional
public class TradeManagerImpl extends EntityManagerImpl<Trade, String> implements
		TradeManager {
	@Autowired
	private SessionManager sessionManager;
	
	/**
	 * 
	 */
	public Trade postAd(Trade trade, User user) {

		//If there is no party associated with user, create a new party.
		user = (User) sessionManager.loadById(User.class, user.getId());
		if (user.getParty() == null) {
			user.setParty(new Party());
			sessionManager.saveOrUpdate(user);
		}
		
		trade.setOwnerParty(user.getParty());
		
		return this.saveEntity(trade);
	}
	/**
	 * 
	 */
	@Transactional(readOnly = true)
	public List<Trade> listTrades(Trade trade, Page page) {
		
		StringBuffer select = new StringBuffer("select trade");
		StringBuffer from = new StringBuffer(" from Trade as trade, Product as p");
		StringBuffer where = new StringBuffer(" where trade.product.id = p.id");
		List params = new ArrayList();
		
		if (trade != null && trade.getTradeType()!=null && !trade.getTradeType().equals("")) {
			where.append(" and trade.tradeType.id = ?");
			params.add(trade.getTradeType().getId());
		}
		if (trade != null && trade.getTitle()!=null && !"".equals(trade.getTitle().trim())) {
			where.append(" and (trade.title like ? or p.longDescription like ?)");
			params.add("%"+trade.getTitle()+"%");
			params.add("%"+trade.getTitle()+"%");
		}
		if (trade != null && trade.getCommercialFlag()!=null && !"-1".equals(trade.getCommercialFlag().trim())) {
			where.append(" and trade.commercialFlag = ?");
			params.add(trade.getCommercialFlag());
		}
		if (trade != null && trade.getGeoCity()!=null && !trade.getGeoCity().getId().equals(new Long(-1))) {
			where.append(" and trade.geoCity.id = ?");
			params.add(trade.getGeoCity().getId());
		}		
		if(trade!=null && trade.getProduct()!=null &&trade.getProduct().getPrimaryCategory()!=null) {
			from.append(", Category as cat");
			where.append(" and ((p.primaryCategory.id = ? and p.primaryCategory.id = cat.id) or (p.primaryCategory.id = cat.id and cat.primaryParentCategory.id = ?))");
			params.add(trade.getProduct().getPrimaryCategory().getId());
			params.add(trade.getProduct().getPrimaryCategory().getId());
		} 
		
		if (trade != null && trade.getFeatureIds()!=null) {
			List<String> featureList = new ArrayList();
			for (String featureId:trade.getFeatureIds()) {
				if (featureId != null && !"-1".equals(featureId.trim()))
					featureList.add(featureId);
			}
			if (!featureList.isEmpty()) {
				for (String featureId:featureList) {
					where.append(" and ? in (select pfa.feature.id from ProductFeatureAppl as pfa where pfa.product.id = p.id)");
					params.add(featureId);
				}
			}
		}
		
		if (page.getSortColumn()==null || "".equals(page.getSortColumn().trim())) {
			page.setSortColumn("trade.createdDate");
			page.setSortDirection("desc");
		}
		
		return this.findByHql(select.toString(), from.append(where).toString(), params.toArray(), page, null);

	}
	/**
	 * 
	 */
	@Transactional(readOnly = true)
	public List<Trade> listTradesByJdbc(Trade trade, Page page) {
		
		return null;

	}
}
